﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace HLSL
{
    
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Effect effect1;
        List<CModel> models;
        CCamera camera;

       
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 1000;
            graphics.PreferredBackBufferHeight = 800;

            Content.RootDirectory = "Content";
        }

      
        protected override void Initialize()
        {
            models = new List<CModel>();
            base.Initialize();
        }

     
        protected override void LoadContent()
        {
          
            spriteBatch = new SpriteBatch(GraphicsDevice);
            effect1 = Content.Load<Effect>("SimpleEffect");

            models.Add(new CModel(Content.Load<Model>("teapot"), new Vector3(0, 500, 0), new Vector3(0, 0, 0), new Vector3(40), GraphicsDevice));
            models.Add(new CModel(Content.Load<Model>("Ground"), new Vector3(0, 0, 0), new Vector3(0, 0, 0), new Vector3(1.0f), GraphicsDevice));
            //Set cho model 1 effect
            //Sau đó nó sẽ qua hàm Draw mà sẽ vẽ theo cách  nào
            //Để có thể tính toán đc các giá trị của ModelMeshPart Effect
            models[0].SetModelEffect(effect1, true);
            models[1].SetModelEffect(effect1, true);
            LightingMaterial mat = new LightingMaterial();
            models[0].Material = mat;
            models[1].Material = mat;
            //mat.AmbientColor = Color.Red.ToVector3();
            mat.LightColor = Color.Green.ToVector3() * 1;
            mat.LightDirection = new Vector3(1, 1, 1);
           
            camera = new FreeCamera(new Vector3(0, 300, 1600), MathHelper.ToRadians(0), MathHelper.ToRadians(5), GraphicsDevice);
        }

      
        protected override void UnloadContent()
        {
            
        }

      
        protected override void Update(GameTime gameTime)
        {
           
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            UpdateFreeCamera();
            UpdateModel(gameTime);
            base.Update(gameTime);
        }
        public void UpdateModel(GameTime gameTime)
        {
            float temp = (float)gameTime.ElapsedGameTime.Milliseconds / 1000;
            models[0].Rotation += new Vector3(temp,0,0);
        }
        public void UpdateFreeCamera()
        {
            KeyboardState keystate = Keyboard.GetState();
            if (keystate.IsKeyDown(Keys.Left))
                ((FreeCamera)camera).Move(Vector3.Left);
            if (keystate.IsKeyDown(Keys.Right))
                ((FreeCamera)camera).Move(Vector3.Right);
            if (keystate.IsKeyDown(Keys.Up))
                ((FreeCamera)camera).Move(Vector3.Up);
            if (keystate.IsKeyDown(Keys.Down))
                ((FreeCamera)camera).Move(Vector3.Down);
            camera.Update();
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            foreach (var model in models)
            {
                if (camera.BoudingVolumeslnView(model.BoudingSphere))
                {
                    model.Draw(camera.View, camera.Projection,((FreeCamera)camera).Position);    
                }
            }
            base.Draw(gameTime);
        }
    }
}
